package com.jzy.stock.stockRoom.service.Impl;

import com.jzy.stock.stockRoom.mapper.StockMapper;
import com.jzy.stock.stockRoom.model.*;
import com.jzy.stock.stockRoom.service.StockService;
import com.jzy.stock.util.AllRecordsVo;
import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import com.jzy.stock.util.StatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author 贾志宇
 * @create 2022-07-11-17:00
 */
@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private StockMapper stockMapper;

    @Override
    public ResultVo updateStatus(@Valid UpdateStatusVo updateStatusVo) {
        StockRoomIdentifyVo stockRoomIdentifyVo = stockMapper.quaryById(updateStatusVo.getId());
        if (stockRoomIdentifyVo == null) {
            return ResultBuildVo.error("修改数据不存在！");
        }
        String name="张三";
        Date nowDate = new Date();
        stockRoomIdentifyVo.setUpdateName(name);
        stockRoomIdentifyVo.setUpdateTime(nowDate);
        BeanUtils.copyProperties(updateStatusVo,stockRoomIdentifyVo);
        stockMapper.updateStatus(stockRoomIdentifyVo);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo add(@Valid IdentifyAddVo identifyAddVo) {
        StockRoomIdentifyVo stockRoomIdentifyVo = stockMapper.quaryByName(identifyAddVo.getName());
        if (stockRoomIdentifyVo != null) {
            return ResultBuildVo.error("库房标识名称已存在");
        }
        StockRoomIdentifyVo stockRoomIdentifyVo1 = stockMapper.quaryByCode(identifyAddVo.getCode());
        if (stockRoomIdentifyVo1 != null) {
            return ResultBuildVo.error("库房标识编码已存在");
        }
        String name="张三";
        Date nowDate = new Date();
        StockRoomIdentifyVo stockRoomIdentifyVo2 = new StockRoomIdentifyVo();
        stockRoomIdentifyVo2.setCreateName(name);
        stockRoomIdentifyVo2.setCreateTime(nowDate);
        BeanUtils.copyProperties(identifyAddVo,stockRoomIdentifyVo2);
        stockMapper.add(stockRoomIdentifyVo2);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo update(@Valid IdentifyUpdateVo identifyUpdateVo) {
        StockRoomIdentifyVo stockRoomIdentifyVo = stockMapper.quaryByName(identifyUpdateVo.getName());
        if (stockRoomIdentifyVo != null) {
            return ResultBuildVo.error("库房标识名称已存在");
        }
        String name="张三";
        Date nowDate = new Date();
        StockRoomIdentifyVo stockRoomIdentifyVo2 = new StockRoomIdentifyVo();
        stockRoomIdentifyVo2.setUpdateName(name);
        stockRoomIdentifyVo2.setUpdateTime(nowDate);
        BeanUtils.copyProperties(identifyUpdateVo,stockRoomIdentifyVo2);
        stockMapper.update(stockRoomIdentifyVo2);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo update1(@Valid StockRoomUpdateVo stockRoomUpdateVo) {
        StockRoomVo stockRoomVo = stockMapper.quaryByName1(stockRoomUpdateVo.getName());
        if (stockRoomVo != null) {
            return ResultBuildVo.error("库房名称已存在");
        }
        String name="张三";
        Date nowDate = new Date();
        StockRoomVo stockRoomVo2 = new StockRoomVo();
        stockRoomVo2.setUpdateName(name);
        stockRoomVo2.setUpdateTime(nowDate);
        BeanUtils.copyProperties(stockRoomUpdateVo,stockRoomVo2);
        stockMapper.update1(stockRoomVo2);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo queryPage(@Valid IdentifySelectVo identifySelectVo) {
        List<StockRoomIdentifyVo> stockRoomIdentifyVos = stockMapper.queryPage(identifySelectVo);
        for(StockRoomIdentifyVo stockRoomIdentifyVo : stockRoomIdentifyVos){
            Integer i = stockRoomIdentifyVo.getStatus() ? 1 : 0;
            stockRoomIdentifyVo.setStatusName(StatusEnum.queryById(i));
        }
        int count = stockMapper.queryCount(identifySelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(stockRoomIdentifyVos);
        allRecordsVo.setPageIndex(identifySelectVo.getPageIndex());
        allRecordsVo.setPageSize(identifySelectVo.getPageSize());
        allRecordsVo.setTotalNumber(count);
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo queryPage1(@Valid StockRoomSelectVo stockRoomSelectVo) {
        List<StockRoomVo> stockRoomVos = stockMapper.queryPage1(stockRoomSelectVo);
        for(StockRoomVo stockRoomVo : stockRoomVos){
            Integer i = stockRoomVo.getStatus() ? 1 : 0;
            stockRoomVo.setStatusName(StatusEnum.queryById(i));
        }
        int count = stockMapper.queryCount1(stockRoomSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(stockRoomVos);
        allRecordsVo.setPageIndex(stockRoomSelectVo.getPageIndex());
        allRecordsVo.setPageSize(stockRoomSelectVo.getPageSize());
        allRecordsVo.setTotalNumber(count);
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo updateStatus1(@Valid UpdateStatusVo updateStatusVo) {
        StockRoomVo stockRoomVo = stockMapper.quaryById1(updateStatusVo.getId());
        if (stockRoomVo == null) {
            return ResultBuildVo.error("修改数据不存在！");
        }
        String name="张三";
        Date nowDate = new Date();
        stockRoomVo.setUpdateName(name);
        stockRoomVo.setUpdateTime(nowDate);
        BeanUtils.copyProperties(updateStatusVo,stockRoomVo);
        stockMapper.updateStatus1(stockRoomVo);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo queryWarehouseTagList() {
        List<StockRoomIdentifyVo> stockRoomIdentifyVos = stockMapper.queryWarehouseTagList();
        return ResultBuildVo.success(stockRoomIdentifyVos);
    }

    @Override
    public ResultVo add1(@Valid StockRoomAddVo stockRoomAddVo) {
        StockRoomVo stockRoomVo = stockMapper.quaryByName1(stockRoomAddVo.getName());
        if (stockRoomVo != null) {
            return ResultBuildVo.error("库房标识名称已存在");
        }
        StockRoomVo stockRoomVo1 = stockMapper.quaryByCode1(stockRoomAddVo.getCode());
        if (stockRoomVo1 != null) {
            return ResultBuildVo.error("库房标识编码已存在");
        }
        String name="张三";
        Date nowDate = new Date();
        StockRoomVo stockRoomVo2 = new StockRoomVo();
        stockRoomVo2.setCreateName(name);
        stockRoomVo2.setCreateTime(nowDate);
        BeanUtils.copyProperties(stockRoomAddVo,stockRoomVo2);
        stockMapper.add1(stockRoomVo2);
        return ResultBuildVo.success();
    }
}
